package org.dsc.amafinder;

import org.dsc.amafinder.model.AmfQR;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@SuppressLint("SetJavaScriptEnabled")
public class AWebView extends Activity {
   private static final String TAG = "afr." + AWebView.class.getSimpleName();
   private WebView mWebView;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Log.i(TAG, "onCreate::" + toString());
      setContentView(R.layout.awebview);

      mWebView = (WebView) findViewById(R.id.awebview);
      mWebView.setWebViewClient(new AWebViewClient());
      android.webkit.WebSettings webSettings = mWebView.getSettings();
      webSettings.setJavaScriptEnabled(true);
      webSettings.setAllowFileAccess(false);
      webSettings.setAllowContentAccess(false);
      mWebView.loadUrl(AmfQR.url);
   }

   public boolean seguirEnVentana(String url) {
      Intent i = new Intent(this, AWebView.class);
      i.putExtra("URL", url);
      startActivity(i);
      return true;
   }
   
   @Override
   protected void onResume() {
      super.onResume();
      Log.i(TAG, "onResume::" + toString());      
   }

   @Override
   protected void onRestart() {
      super.onRestart();
      Log.i(TAG, "onRestart::" + toString());
   }   

   private class AWebViewClient extends WebViewClient {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
         if (!url.contains("/s/")) {
            url = url + "&ie=UTF8&camp=3626&force-full-site=1&ref_=aw_bottom_links&linkCode=as2&tag=articuinteree-21";
            Uri uri = Uri.parse(url);
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(browserIntent);
         } else {
            seguirEnVentana(url);
         }
         Log.i(TAG, "shouldOverrideUrlLoading " + url);
         return true;
      }
   }

   /*
    * @ini-menu-action-bar
    */
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu items for use in the action bar
      getMenuInflater().inflate(R.menu.main_activity_actions, menu);
      menu.findItem(R.id.action_search).setIntent(new Intent(this, AMain.class));
      menu.findItem(R.id.action_options).setIntent(new Intent(this, AOptions.class));
      menu.findItem(R.id.action_info).setIntent(new Intent(this, AInfo.class));
      return super.onCreateOptionsMenu(menu);
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.action_search:
      case R.id.action_options:
      case R.id.action_info:
         finish();
         startActivity(item.getIntent());
         break;
      default:
         return super.onOptionsItemSelected(item);
      }
      return true;
   }
   /*
    * @end-menu-action-bar
    */
}
